using Circus.CodeEditor;

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace Circus.Syntax.CodeCompletion
{
	public class CodeSnippetMembers : CodeCompletionProvider, ICodeSnippetsProvider, ICodeCompletionProvider, IList<IComparable>
	{
		private ICodeSnippetMember parent;
		public virtual ICodeSnippetMember Parent
		{
			get
			{
				return this.parent;
			}
			set
			{
				if (this.parent != value)
				{
					this.parent = value;
					this.OnParentChanged();
				}
			}
		}
		public new ICodeSnippetMember this[int index]
		{
			get
			{
				return base[index] as ICodeSnippetMember;
			}
			set
			{
				base[index] = value;
			}
		}
		public override int ColumnCount
		{
			get
			{
				return 1;
			}
		}
		public override ImageList Images
		{
			get
			{
				return (base.Images != null) ? base.Images : ((this.GetParent() != null) ? this.GetParent().Images : null);
			}
			set
			{
				base.Images = value;
			}
		}
		public override string EditPath
		{
			get
			{
				return (this.parent != null) ? this.parent.EditPath : string.Empty;
			}
		}
	
		protected virtual void OnParentChanged()
		{
		}
		public CodeSnippetMembers()
		{
			this.UseIndent = false;
			this.EditField = StringConsts.CodeSnippetsEditField;
		}
		public CodeSnippetMembers(ICodeSnippetMember parent) : this()
		{
			this.parent = parent;
		}
		public virtual ICodeSnippetMember AddSnippetMember()
		{
			ICodeSnippetMember codeSnippetMember = new CodeSnippetMember(this);
			base.Add(codeSnippetMember);
			return codeSnippetMember;
		}
		public virtual ICodeSnippetMember InsertSnippetMember(int index)
		{
			ICodeSnippetMember codeSnippetMember = new CodeSnippetMember();
			base.Insert(index, codeSnippetMember);
			return codeSnippetMember;
		}
		public override void OnClosePopup(object sender, ClosingEventArgs e)
		{
			if (e.Accepted && this.SelIndex >= 0 && this.SelIndex < base.Count)
			{
				e.Provider = this[this.SelIndex].SnippetsAndMembers;
				e.Handled = (e.Provider.Count > 0);
			}
			base.OnClosePopup(sender, e);
		}
		public override int GetImageIndex(int index)
		{
			return this[index].ImageIndex;
		}
		public virtual CodeSnippet FindByShortcut(string shortcut, bool caseSensitive)
		{
			CodeSnippet result;
			using (List<IComparable>.Enumerator enumerator = base.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ICodeSnippetMember codeSnippetMember = (ICodeSnippetMember)enumerator.Current;
					CodeSnippet codeSnippet = codeSnippetMember.Snippets.FindByShortcut(shortcut, caseSensitive);
					if (codeSnippet != null)
					{
						result = codeSnippet;
						return result;
					}
					codeSnippet = codeSnippetMember.Members.FindByShortcut(shortcut, caseSensitive);
					if (codeSnippet != null)
					{
						result = codeSnippet;
						return result;
					}
				}
			}
			result = null;
			return result;
		}
		public virtual bool ShouldIndent(CodeSnippet snippet)
		{
			return this.UseIndent;
		}
		public override bool ColumnVisible(int column)
		{
			return true;
		}
		public override string GetColumnText(int index, int column)
		{
			return (column == 0) ? this.GetName(index) : string.Empty;
		}
		public override string GetText(int index)
		{
			return string.Empty;
		}
		public override string GetName(int index)
		{
			return this[index].Name;
		}
		public override ICodeCompletionProvider GetParent()
		{
			return (this.parent != null) ? this.parent.Parent : null;
		}
	}
}
